package com.szholly.plug.safe.core;

import java.util.HashMap;
import java.util.List;

import com.szholly.data.general.IQueryFilter;
import com.szholly.data.general.QueryFilter;
import com.szholly.data.general.RowBase;
import com.szholly.plug.safe.entity.workday.HolidaysEntity;
import com.szholly.plug.safe.entity.workday.HolidaysService;
import com.szholly.plug.safe.entity.workday.WorkDayHelper;
import com.szholly.plug.safe.entity.workday.WorkDayUtils;
import com.szholly.plug.safe.entity.workday.WorkTimeEntity;
import com.szholly.plug.safe.entity.workday.WorkTimeService;
/**
 * 节假日与工作时间缓存类
 * @author wy
 *
 */
public class WorkDayCache {

	/**
	 * 重新加载并缓存工作时间设置
	 */
	public static void reLoad() {
		WorkDayHelper.setHolidaysCache(null);
		WorkDayHelper.setWorkTimesCache(null);
		init();
	}	
	
	/**
	 * 缓存设置工作时间
	 */
	public static void init() {
		initHolidays();
		initWorkTime();
	}
	/**
	 * 初始化缓存节假日
	 */
	private static void initHolidays() {
		HashMap<String, RowBase> workDays = WorkDayHelper.getHolidaysCache();
		if (workDays == null || workDays.size() == 0) {
			workDays = new HashMap<String, RowBase>();
			IQueryFilter queryFilter = new QueryFilter();
			List<HolidaysEntity> holidays = HolidaysService.getSingleRef()
					.getEntityList(HolidaysEntity.class, queryFilter);
			for (HolidaysEntity rowBase : holidays) {
				String key = WorkDayUtils.formatDate(rowBase.getHDate(),
						WorkDayUtils.SIMPLE_FORMAT);
				workDays.put(key, rowBase);
			}
			WorkDayHelper.setHolidaysCache(workDays);
		}
	}
	/**
	 * 初始化缓存工作时间
	 */
	private static void initWorkTime() {
		HashMap<String, String> workTimes = WorkDayHelper.getWorkTimesCache();
		if (workTimes == null || workTimes.size() == 0) {
			workTimes = new HashMap<String, String>();
			IQueryFilter queryFilter = new QueryFilter();
//			String whereString = WorkTimeEntity.FIELD_C_TYPE + " = 'S'";// 系统工作时间
//			queryFilter.setWhereString(whereString);
			WorkTimeEntity times = WorkTimeService.getSingleRef().getEntity(
					WorkTimeEntity.class, queryFilter);
			if (times != null) {
				workTimes.put(WorkTimeEntity.FIELD_S_MON_START, times.getSMonStart());
				workTimes.put(WorkTimeEntity.FIELD_S_MON_END, times.getSMonEnd());
				workTimes.put(WorkTimeEntity.FIELD_S_AM_START, times.getSAmStart());
				workTimes.put(WorkTimeEntity.FIELD_S_AM_END, times.getSAmEnd());
				workTimes.put(WorkTimeEntity.FIELD_S_PM_START, times.getSPmStart());
				workTimes.put(WorkTimeEntity.FIELD_S_PM_END, times.getSPmEnd());
				
				workTimes.put(WorkTimeEntity.FIELD_W_MON_START, times.getWMonStart());
				workTimes.put(WorkTimeEntity.FIELD_W_MON_END, times.getWMonEnd());
				workTimes.put(WorkTimeEntity.FIELD_W_AM_START, times.getWAmStart());
				workTimes.put(WorkTimeEntity.FIELD_W_AM_END, times.getWAmEnd());
				workTimes.put(WorkTimeEntity.FIELD_W_PM_START, times.getWPmStart());
				workTimes.put(WorkTimeEntity.FIELD_W_PM_END, times.getWPmEnd());
			} else {
				workTimes.put(WorkTimeEntity.FIELD_S_MON_START, "5");
				workTimes.put(WorkTimeEntity.FIELD_S_MON_END, "10");
				workTimes.put(WorkTimeEntity.FIELD_S_AM_START, "08:00");
				workTimes.put(WorkTimeEntity.FIELD_S_AM_END, "12:00");
				workTimes.put(WorkTimeEntity.FIELD_S_PM_START, "14:00");
				workTimes.put(WorkTimeEntity.FIELD_S_PM_END, "18:00");
				
				workTimes.put(WorkTimeEntity.FIELD_W_MON_START, "11");
				workTimes.put(WorkTimeEntity.FIELD_W_MON_END, "4");
				workTimes.put(WorkTimeEntity.FIELD_W_AM_START, "08:00");
				workTimes.put(WorkTimeEntity.FIELD_W_AM_END, "12:00");
				workTimes.put(WorkTimeEntity.FIELD_W_PM_START, "13:00");
				workTimes.put(WorkTimeEntity.FIELD_W_PM_END, "17:00");
			}
			WorkDayHelper.setWorkTimesCache(workTimes);
		}
	}
}
